package gui;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 */
public class ConnectToServerPanel extends JPanel
{
    private final DasIrcWindow window_;

    private final String DEFAULTHOST = "se.quakenet.org";
    private final String DEFAULTPORT = "6667";

    private final JLabel hostLabel_ = new JLabel( "Host:" );
    private final JLabel portLabel_ = new JLabel( "Port:" );
    private final JLabel nickLabel_ = new JLabel( "Nick:" );

    private final JTextField hostField_ = new JTextField( DEFAULTHOST );
    private final JTextField portField_ = new JTextField( DEFAULTPORT );
    private final JTextField nickField_ = new JTextField();

    private final JButton connectButton_ = new JButton( "Connect" );
    private final JButton cancelButton_ = new JButton( "Cancel" );

    public ConnectToServerPanel( DasIrcWindow window )
    {
        super( new GridLayout( 4, 4 ) );

        window_ = window;

        cancelButton_.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent e )
            {
                window_.switchToChatPanel();
            }
        });

        connectButton_.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent e )
            {
                boolean isValid = true;
                int port = -1;

                // Validate the host-field
                if( hostField_.getText().isEmpty() )
                {
                    hostField_.setBackground( Color.red );
                    isValid = false;
                }
                else
                    hostField_.setBackground( Color.white );

                // Validate the port-field
                if( portField_.getText().isEmpty() )
                {
                    portField_.setBackground( Color.red );
                    isValid = false;
                }
                else
                {
                    try
                    {
                        port = Integer.parseInt( portField_.getText() );
                        portField_.setBackground( Color.white );
                    }
                    catch( NumberFormatException ex )
                    {
                        portField_.setBackground( Color.red );
                        isValid = false;
                    }
                }

                // Validate the nick-field
                if( nickField_.getText().isEmpty() )
                {
                    nickField_.setBackground( Color.red );
                    isValid = false;
                }
                else
                    nickField_.setBackground( Color.WHITE );

                // If everything is valid, connect to the server!
                if( isValid )
                {
                    window_.switchToChatPanel();
                    window_.createConnection( hostField_.getText(),
                            port,
                            nickField_.getText() );
                    reset();
                }
            }
        });

        add( hostLabel_ );
        add( hostField_ );

        add( portLabel_ );
        add( portField_ );

        add( nickLabel_ );
        add( nickField_ );

        add( cancelButton_ );
        add( connectButton_ );

        repaint();
    }

    public final void reset()
    {
        hostField_.setText( DEFAULTHOST );
        portField_.setText( DEFAULTPORT );
        nickField_.setText( null );

        hostLabel_.setBackground( Color.white );
        portLabel_.setBackground( Color.white );
        nickLabel_.setBackground( Color.white );
    }
}
